package persistence;

import java.sql.*;

public class DBUtil {
    private static String driveString = "com.mysql.cj.jdbc.Driver";
    private static String connectionString = "jdbc:mysql://localhost:3306/mypetstore";
    private static String username = "root";
    private static String password = "**";

    //获取连接
    public static Connection getConnection(){
        Connection connection=null;

        try {
            Class.forName(driveString);
            connection = DriverManager.getConnection(connectionString,username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }

    public static void closeConnection(Connection connection) {
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void closeStatement(Statement statement){
        if(statement!=null) {
            try {
                statement.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void closePreparedStatement(PreparedStatement preparedStatement){
        if(preparedStatement!=null) {
            try {
                preparedStatement.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void closeResultSet(ResultSet resultSet){
        if(resultSet!=null) {
            try {
                resultSet.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    //关闭连接

    //test the db connection
//    public static void main(String[] args){
//        System.out.println(getConnection());
//    }
//
}

